package com.ocm.warehouse.feign;

import com.ocm.warehouse.bo.StockLockBO;
import com.ocm.warehouse.service.IStockService;
import lombok.AllArgsConstructor;
import com.ocm.core.log.exception.ServiceException;
import com.ocm.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;

/**
 * Author:   hqy
 * Date:     2021/12/4 22:18
 * Description:
 */
@ApiIgnore
@RestController
@AllArgsConstructor
public class StockClient implements IStockClient{

	IStockService stockService;

	@Override
	@GetMapping(API_PREFIX+"hasStockBySpuId")
	public R<Boolean> hasStockBySpuId(@NotNull Long spuId) {
		return R.data(stockService.getStockMin(spuId));
	}

	@Override
	@PostMapping(API_PREFIX+"stockLock")
	public R<Long> stockLock(StockLockBO stockLocks) {
		try {
			stockService.stockLock(stockLocks,stockLocks.getOrderId());

		}catch (ServiceException e){
			return R.data(Long.parseLong(e.getMessage()));
		}
		return R.data(0L);
	}

	@Override

	@PostMapping(API_PREFIX+"stockUnlock")
	public R stockUnLock(Long orderId) {
		stockService.stockUnlock(orderId);
		return R.success("");
	}
}
